<template>
    <div class="login-container">
      <div class="login-form">
        <h2 class="title">用户登录</h2>
        <el-form
          ref="loginFormRef"
          :model="loginForm"
          :rules="loginRules"
          class="form"
          label-position="top"
        >
          <el-form-item label="用户名" prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            />
          </el-form-item>
          
          <el-form-item label="密码" prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              placeholder="请输入密码"
              :prefix-icon="Lock"
              show-password
              @keyup.enter="handleLogin"
            />
          </el-form-item>
          
          <el-form-item>
            <el-checkbox>记住我</el-checkbox>
            <el-link type="primary" class="forget-pwd" @click="handleForget">忘记密码?</el-link>
          </el-form-item>
          
          <el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              @click="handleLogin"
            >
              登录
            </el-button>
          </el-form-item>
          
          <div class="register-tip">
            还没有账号? <el-link type="primary" @click="handleRegister">立即注册</el-link>
          </div>
        </el-form>
      </div>
    </div>
  </template>
  
  <script setup>
  import { ref,  reactive } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElLoading, ElMessage } from 'element-plus';
  import { User, Lock } from '@element-plus/icons-vue'
  import {login} from '@/util/request.js'
  
  const router = useRouter()
  // 表单引用
  const loginFormRef = ref(null)
  
  // 响应式数据
  const loginForm = reactive({
    username: '',
    password: ''
  })
   
  const loginRules = ref({
    username: [
      { required: true, message: '请输入用户名', trigger: 'blur' },
      { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入密码', trigger: 'blur' },
      { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
    ]
  })
 import {useCounterStore} from '@/stores/counter.js'
  const counterStore = useCounterStore()
  //登录处理方法
  const handleLogin = async () => {
  if (!loginForm.username || !loginForm.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  const loading = ElLoading.service({
    lock: true,
    text: '正在登录...',
    background: 'rgba(0, 0, 0, 0.1)'
  })
  
  try {
    const { data } = await login({
      username: loginForm.username.trim(),
      password: loginForm.password
    })
    console.log("userId:",data.data.userId)
    console.log("token:",data.data.token)
    // 确保返回数据结构正确
    if (!data || !data.data.token || !data.data.userId) {
      throw new Error('登录返回数据不完整')
    }
    console.log("data:",data)
    ElMessage.success('登录成功')
    
    // 存储用户信息
    counterStore.login(data.data.token, {
      id: data.data.userId
    })
    
    router.push({ name: 'love' })
  } catch (error) {
    ElMessage.error(error.response?.data?.message || '登录失败')
    console.error('登录错误:', error)
  } finally {
    loading.close()
  }
}
  
  const handleForget = () => {
    router.push('/forget-password')
  }
  
  const handleRegister = () => {
    router.push({ name: 'Register' })
  }
  </script>
  
  <style>
  .login-container {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background-color: #f0f2f5;
    background-image: url('~@/assets/login-bg.jpg');
    background-size: cover;
  }
  
  .login-form {
    width: 420px;
    padding: 40px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  }
  
  .login-form .title {
    margin-bottom: 30px;
    text-align: center;
    color: #333;
  }
  
  .login-form .form .forget-pwd {
    float: right;
  }
  
  .login-form .form .login-btn {
    width: 100%;
    margin-top: 10px;
  }
  
  .login-form .register-tip {
    margin-top: 20px;
    text-align: center;
    font-size: 14px;
    color: #666;
  }
  </style>